use std::io;

use thiserror::Error;
use htmd::HtmlToMarkdown;




#[derive(Debug, Error)]
pub enum MarkdownError {
    #[error("HTML to Markdown conversion error: {0}")]
    IoError(#[from] io::Error)
}

pub fn convert(html: &str) -> Result<String, MarkdownError> {
    let converter = HtmlToMarkdown::builder()
        .skip_tags(vec!["script", "style", "head"])
        .build();

    Ok(converter.convert(html)?)
}



#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_html_to_md() -> Result<(), MarkdownError> {
        let target_file_path = "/data/test/use.html";
        let content = std::fs::read_to_string(target_file_path)?;
        let converted = convert(&content);
        let output_path = "/data/test/use.md";
        std::fs::write(output_path, converted?)?;
        Ok(())
    }
}